package com.hopu.oa.controller.mail;

import com.aliyun.oss.OSSClient;
import com.aliyun.oss.model.GetObjectRequest;
import com.hopu.oa.entity.mail.AoaMailnumber;
import com.hopu.oa.entity.mail.Inmaillist;
import com.hopu.oa.entity.mail.Mailreciver;
import com.hopu.oa.entity.mail.Pagemail;
import com.hopu.oa.entity.management.AoaAttendsList;
import com.hopu.oa.entity.management.AoaStatusList;
import com.hopu.oa.entity.management.AoaTypeList;
import com.hopu.oa.entity.plan.AttachmentList;
import com.hopu.oa.entity.process.AoaAttachmentList;
import com.hopu.oa.entity.user.AoaUser;
import com.hopu.oa.service.impl.mail.mailServiceImpl;
import com.hopu.oa.service.impl.process.NewProcessServiceImp2;
import com.hopu.oa.service.impl.process.auditServiceImpl;
import com.hopu.oa.service.impl.user.UserServiceImpl;
import com.hopu.oa.until.OosUtil;
import com.hopu.oa.until.data.AliyunOSSUtil;
import com.hopu.oa.until.data.ConstantProperties;
import com.hopu.oa.until.data.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.*;


@Controller
public class mailController {
    @Autowired
    private mailServiceImpl mailService;
    @Autowired
    private auditServiceImpl auditService;
    @Autowired
    private UserServiceImpl userService;
    //工具类，文件上传
    @Autowired
    private NewProcessServiceImp2 newProcessService2;
    @Autowired
    private OosUtil oosUtil;
    @Autowired
    private ConstantProperties constantProperties;
    @Autowired
    private AliyunOSSUtil aliyunOSSUtil;

    /**
     * 邮件管理主页
     *
     * @return
     */
    @RequestMapping("mail")
    public String mail(HttpSession session, Model model,
                       String find,
                       @RequestParam(value = "page", defaultValue = "1") int page,
                       @RequestParam(value = "size", defaultValue = "10") int size) {
        System.out.println("find = " + find);
        //查找用户
        AoaUser user = (AoaUser) session.getAttribute("user");
        //查找未读邮件
        List<Mailreciver> noreadlist = mailService.findByReadAndDelAndReciverId(0, 0, user.getUserId());
        //查找创建了但是却没有发送的邮件
        List<Inmaillist> nopushlist = mailService.findByPushAndDelAndMailUserid(0, 0, user.getUserId());
        //查找发件条数
        List<Inmaillist> pushlist = mailService.findByPushAndDelAndMailUserid(1, 0, user.getUserId());
        //查找收件箱删除的邮件条数
        List<Mailreciver> rubbish = mailService.findByDelAndReciverId(1, user.getUserId(), user.getUserName());
        //分页及查找

        int totalElements = mailService.recive(find, page, 0, user.getUserId()).size();
        //有几页
        int totalPages = totalElements % size == 0 ? totalElements / size : totalElements / size + 1;
        List<Pagemail> pagelist = mailService.recive(find, (page - 1) * size, size, user.getUserId());
        HashMap<String, Integer> map1 = new HashMap<>();
        map1.put("totalElements", totalElements);
        map1.put("totalPages", totalPages);
        map1.put("number", page);
        map1.put("size", size);

        model.addAttribute("page", map1);
        model.addAttribute("maillist", pagelist);
        model.addAttribute("find", find);
        model.addAttribute("url", "mailbody");

        model.addAttribute("noread", noreadlist.size());
        model.addAttribute("nopush", nopushlist.size());
        model.addAttribute("push", pushlist.size());
        model.addAttribute("rubbish", rubbish.size());
        model.addAttribute("mess", "收件箱");
        model.addAttribute("sort", "&title=收件箱");
        return "mail/mail";
    }

    @RequestMapping("mailbody")
    public String mailbody(Model model, HttpSession session,
                           String find,
                           @RequestParam(value = "page", defaultValue = "1") int page,
                           @RequestParam(value = "size", defaultValue = "10") int size
    ) {
        System.out.println("find = " + find);
        //查找用户
        AoaUser user = (AoaUser) session.getAttribute("user");
        //查找未读邮件
        List<Mailreciver> noreadlist = mailService.findByReadAndDelAndReciverId(0, 0, user.getUserId());
        //查找创建了但是却没有发送的邮件
        List<Inmaillist> nopushlist = mailService.findByPushAndDelAndMailUserid(0, 0, user.getUserId());
        //查找发件条数
        List<Inmaillist> pushlist = mailService.findByPushAndDelAndMailUserid(1, 0, user.getUserId());
        //查找收件箱删除的邮件条数
        List<Mailreciver> rubbish = mailService.findByDelAndReciverId(1, user.getUserId(), user.getUserName());

        //分页及查找
        int totalElements = mailService.recive(find, page, 0, user.getUserId()).size();
        //有几页
        int totalPages = totalElements % size == 0 ? totalElements / size : totalElements / size + 1;
        List<Pagemail> pagelist = mailService.recive(find, (page - 1) * size, size, user.getUserId());
        HashMap<String, Integer> map1 = new HashMap<>();
        map1.put("totalElements", totalElements);
        map1.put("totalPages", totalPages);
        map1.put("number", page);
        map1.put("size", size);

        model.addAttribute("page", map1);
        model.addAttribute("maillist", pagelist);
        model.addAttribute("find", find);
        model.addAttribute("url", "mailbody");

        model.addAttribute("noread", noreadlist.size());
        model.addAttribute("nopush", nopushlist.size());
        model.addAttribute("push", pushlist.size());
        model.addAttribute("rubbish", rubbish.size());
        model.addAttribute("mess", "收件箱");
        model.addAttribute("sort", "&title=收件箱");
        return "mail/mailbody";
    }

    /**
     * 删除邮件
     */
    @RequestMapping("alldelete")
    public String alldelete(
            String ids,
            Model model,
            HttpSession session,
            String title
    ) {
        //查找用户
        AoaUser user = (AoaUser) session.getAttribute("user");
        String[] str = ids.split(",");
        System.out.println(Arrays.toString(str));
        if (("垃圾箱").equals(title)) {
            System.out.println("垃圾箱删除");
            for (int i = 0; i < str.length; i++) {
                mailService.deletEmail(Integer.parseInt(str[i]));
            }

        } else if (("收件箱").equals(title)) {
            for (int i = 0; i < str.length; i++) {

                //找到该用户联系邮件的中间记录
                Mailreciver mailr = mailService.findbyReciverIdAndmailId(user.getUserId(), Integer.parseInt(str[i]));
                if (!Objects.isNull(mailr)) {
                    //把主表删除的字段改为1
                    System.out.println("mailr = " + mailr + i);
                    mailService.setDel(1, mailr.getMailId());
                    mailService.saveShouDel(1, mailr.getMailId(), user.getUserId());
                }
            }
        } else if (("草稿箱").equals(title)) {

            for (int i = 0; i < str.length; i++) {
                Inmaillist inmaillist = mailService.selectByMailId(Integer.parseInt(str[i]));
                mailService.setDel(1, inmaillist.getMailId());
            }
        } else {
            //发件箱

            for (int i = 0; i < str.length; i++) {
                Inmaillist inmaillist = mailService.selectByMailId(Integer.parseInt(str[i]));
                mailService.saveDel(1, inmaillist.getMailId());
                mailService.setDel(1, inmaillist.getMailId());
            }
        }
        return "redirect:mailbody";
    }

    /**
     * 查看邮件
     */
    @RequestMapping("smail")
    public String Check(Model model, String title, HttpSession session, Integer id) {
        //查找用户
        AoaUser user = (AoaUser) session.getAttribute("user");
        System.out.println("title = " + title);
        System.out.println("id = " + id);
        //找到中间表信息
        if (("收件箱").equals(title) || ("垃圾箱").equals(title)) {
            Mailreciver mailr = mailService.findbyReciverIdAndmailId(user.getUserId(), id);
            mailService.saveRead(1, mailr.getMailId());
        }
        if (("收件箱").equals(title)) {
            Mailreciver mailreciver = mailService.selectByMailreciverbyMailId(id, user.getUserId());
            if (mailreciver.getIsRead() != 1) {
                mailService.updateEmail(id);
            }
        }
        //找到该邮件信息
        Inmaillist inmaillist = mailService.selectByMailId(id);
        if (inmaillist.getMailFileid() != null) {
            AttachmentList attachmentList = mailService.selectAttachmentListByFileId(inmaillist.getMailFileid());
            model.addAttribute("file", attachmentList);
        }
        model.addAttribute("mail", inmaillist);
        model.addAttribute("mess", title);
        return "mail/seemail";
    }

    /**
     * 最近邮件
     */
    @RequestMapping("amail")
    public String index3(HttpSession session, Model model,
                         String find, String title,
                         @RequestParam(value = "page", defaultValue = "1") int page,
                         @RequestParam(value = "size", defaultValue = "10") int size) {
        //查找用户
        AoaUser user = (AoaUser) session.getAttribute("user");
        String mess = title;
        System.out.println("mess = " + mess);
        if (("收件箱").equals(mess)) {
            //分页及查找
            int totalElements = mailService.recive(find, page, 0, user.getUserId()).size();
            //有几页
            int totalPages = totalElements % size == 0 ? totalElements / size : totalElements / size + 1;
            List<Pagemail> pagelist = mailService.recive(find, (page - 1) * size, size, user.getUserId());
            HashMap<String, Integer> map1 = new HashMap<>();

            map1.put("totalElements", totalElements);
            map1.put("totalPages", totalPages);
            map1.put("number", page);
            map1.put("size", size);
            model.addAttribute("page", map1);
            model.addAttribute("maillist", pagelist);
            model.addAttribute("find", find);
            model.addAttribute("url", "index4");
            model.addAttribute("mess", "收件箱");
            model.addAttribute("sort", "&title=收件箱");
        } else if (("发件箱").equals(mess)) {
            //分页及查找
            int totalElements = mailService.send(find, page, 0, user.getUserId()).size();

            //有几页
            int totalPages = totalElements % size == 0 ? totalElements / size : totalElements / size + 1;
            List<Pagemail> pagelist = mailService.send(find, (page - 1) * size, size, user.getUserId());
            HashMap<String, Integer> map1 = new HashMap<>();
            map1.put("totalElements", totalElements);
            map1.put("totalPages", totalPages);
            map1.put("number", page);
            map1.put("size", size);
            for (Pagemail pagemail : pagelist) {
                System.out.println(pagemail);
            }
            model.addAttribute("page", map1);
            model.addAttribute("maillist", pagelist);
            model.addAttribute("find", find);
            model.addAttribute("url", "index4");
            model.addAttribute("mess", "发件箱");
            model.addAttribute("sort", "&title=发件箱");
        } else if (("草稿箱").equals(mess)) {
            //分页及查找
            int totalElements = mailService.draft(find, page, 0, user.getUserId()).size();
            //有几页
            int totalPages = totalElements % size == 0 ? totalElements / size : totalElements / size + 1;
            List<Pagemail> pagelist = mailService.draft(find, (page - 1) * size, size, user.getUserId());
            HashMap<String, Integer> map1 = new HashMap<>();
            map1.put("totalElements", totalElements);
            map1.put("totalPages", totalPages);
            map1.put("number", page);
            map1.put("size", size);
            model.addAttribute("page", map1);
            model.addAttribute("maillist", pagelist);
            model.addAttribute("find", find);
            model.addAttribute("url", "index4");
            model.addAttribute("mess", "草稿箱");
            model.addAttribute("sort", "&title=草稿箱");
        } else {
            //垃圾箱
            //分页及查找
            int totalElements = mailService.trashCan(find, page, 0, user.getUserId(), user.getUserName()).size();
            //有几页
            int totalPages = totalElements % size == 0 ? totalElements / size : totalElements / size + 1;
            List<Pagemail> pagelist = mailService.trashCan(find, (page - 1) * size, size, user.getUserId(), user.getUserName());
            HashMap<String, Integer> map1 = new HashMap<>();
            map1.put("totalElements", totalElements);
            map1.put("totalPages", totalPages);
            map1.put("number", page);
            map1.put("size", size);
            model.addAttribute("page", map1);
            model.addAttribute("maillist", pagelist);
            model.addAttribute("find", find);
            model.addAttribute("url", "index4");
            model.addAttribute("mess", "垃圾箱");
            model.addAttribute("sort", "&title=垃圾箱");
        }


        return "mail/allmail";
    }

    /**
     * 最近邮件2
     */
    @RequestMapping("index4")
    public String index4(HttpSession session, Model model,
                         String find, String title,
                         @RequestParam(value = "page", defaultValue = "1") int page,
                         @RequestParam(value = "size", defaultValue = "10") int size) {
        //查找用户
        AoaUser user = (AoaUser) session.getAttribute("user");
        String mess = title;
        System.out.println("mess = " + mess);
        if (("收件箱").equals(mess)) {
            //分页及查找
            int totalElements = mailService.recive(find, page, 0, user.getUserId()).size();
            //有几页
            int totalPages = totalElements % size == 0 ? totalElements / size : totalElements / size + 1;
            List<Pagemail> pagelist = mailService.recive(find, (page - 1) * size, size, user.getUserId());
            HashMap<String, Integer> map1 = new HashMap<>();

            map1.put("totalElements", totalElements);
            map1.put("totalPages", totalPages);
            map1.put("number", page);
            map1.put("size", size);
            model.addAttribute("page", map1);
            model.addAttribute("maillist", pagelist);
            model.addAttribute("find", find);
            model.addAttribute("url", "index4");
            model.addAttribute("mess", "收件箱");
            model.addAttribute("sort", "&title=收件箱");
        } else if (("发件箱").equals(mess)) {
            //分页及查找
            List<Pagemail> send = mailService.send(find, page, 0, user.getUserId());
            HashSet set = new HashSet(send);
            send.clear();
            send.addAll(set);
            int totalElements = send.size();

            //有几页
            int totalPages = totalElements % size == 0 ? totalElements / size : totalElements / size + 1;
            List<Pagemail> pagelist = mailService.send(find, (page - 1) * size, size, user.getUserId());
            HashMap<String, Integer> map1 = new HashMap<>();
            map1.put("totalElements", totalElements);
            map1.put("totalPages", totalPages);
            map1.put("number", page);
            map1.put("size", size);
            for (Pagemail pagemail : pagelist) {
                System.out.println(pagemail);
            }
//            HashSet maillist = new HashSet(pagelist);
//            pagelist.clear();
//            pagelist.addAll(maillist);
            model.addAttribute("page", map1);
            model.addAttribute("maillist", pagelist);
            model.addAttribute("find", find);
            model.addAttribute("url", "index4");
            model.addAttribute("mess", "发件箱");
            model.addAttribute("sort", "&title=发件箱");
        } else if (("草稿箱").equals(mess)) {
            //分页及查找
            int totalElements = mailService.draft(find, page, 0, user.getUserId()).size();
            //有几页
            int totalPages = totalElements % size == 0 ? totalElements / size : totalElements / size + 1;
            List<Pagemail> pagelist = mailService.draft(find, (page - 1) * size, size, user.getUserId());
            HashMap<String, Integer> map1 = new HashMap<>();
            map1.put("totalElements", totalElements);
            map1.put("totalPages", totalPages);
            map1.put("number", page);
            map1.put("size", size);
            model.addAttribute("page", map1);
            model.addAttribute("maillist", pagelist);
            model.addAttribute("find", find);
            model.addAttribute("url", "index4");
            model.addAttribute("mess", "草稿箱");
            model.addAttribute("sort", "&title=草稿箱");
        } else {
            //垃圾箱
            //分页及查找
            int totalElements = mailService.trashCan(find, page, 0, user.getUserId(), user.getUserName()).size();
            //有几页
            int totalPages = totalElements % size == 0 ? totalElements / size : totalElements / size + 1;
            List<Pagemail> pagelist = mailService.trashCan(find, (page - 1) * size, size, user.getUserId(), user.getUserName());
            HashMap<String, Integer> map1 = new HashMap<>();
            map1.put("totalElements", totalElements);
            map1.put("totalPages", totalPages);
            map1.put("number", page);
            map1.put("size", size);
            model.addAttribute("page", map1);
            model.addAttribute("maillist", pagelist);
            model.addAttribute("find", find);
            model.addAttribute("url", "index4");
            model.addAttribute("mess", "垃圾箱");
            model.addAttribute("sort", "&title=垃圾箱");
        }


        return "mail/mailbody";
    }

    /**
     * 恢复垃圾箱
     */
    @RequestMapping("refresh")
    public String refresh(HttpSession session, Model model, String title, String ids
    ) {
        //查找用户
        AoaUser user = (AoaUser) session.getAttribute("user");
        System.out.println("ids = " + ids);
        String[] str = ids.split(",");
        //收件箱
        System.out.println(Arrays.toString(str));
        for (int i = 0; i < str.length; i++) {
            Mailreciver mailr = mailService.findbyReciverIdAndmailId(user.getUserId(), Integer.parseInt(str[i]));
            System.out.println("mailr = " + mailr);
            if (mailr != null) {
                System.out.println("收件箱");
                mailService.setDel(0, mailr.getMailId());
                mailService.saveShouDel(0, mailr.getMailId(), user.getUserId());
            } else {
                Inmaillist inmaillist = mailService.selectByMailId(Integer.parseInt(str[i]));
                System.out.println(inmaillist);
                if (inmaillist.getMailPush() != 1) {
                    System.out.println("草稿箱");
                    //草稿箱
                    mailService.setDel(0, inmaillist.getMailId());
                } else {
                    System.out.println("发件箱");
                    //发件箱
                    mailService.setDel(0, inmaillist.getMailId());
                    mailService.saveDel(0, inmaillist.getMailId());
                }
            }
        }
        return "redirect:mailbody";
    }

    /**
     * 写信
     */
    @RequestMapping("wmail")
    public String writeALetter(Integer id, Model model, HttpSession session, String huifu,
                               @RequestParam(value = "page", defaultValue = "1") int page,
                               @RequestParam(value = "size", defaultValue = "5") int size
    ) {
        System.out.println("huifu = " + huifu);
        //查找用户
        AoaUser user = (AoaUser) session.getAttribute("user");
        List<AoaMailnumber> aoaMailnumbers = mailService.selectAoaMailnumber(user.getUserId());
        List<AoaTypeList> aoaTypeLists = mailService.selectTypeList();
        List<AoaStatusList> aoaStatusLists = mailService.selectStatusList();
        if (id != null) {
            System.out.println("id = " + id);
            if (("回复").equals(huifu)) {
                //找到该邮件信息
                Pagemail pagemail = mailService.WriteAttachmentListByFileId(id);
                if (pagemail.getAttachment_Id() != null) {
                    AttachmentList attachmentList = mailService.selectAttachmentListByFileId(pagemail.getAttachment_Id());
                    model.addAttribute("file", attachmentList);
                }
                Pagemail mail = new Pagemail();
                mail.setPushMailUserName(pagemail.getPushMailUserName());
                mail.setMailStatusId(pagemail.getMailStatusId());
                mail.setMailStatusName(pagemail.getMailStatusName());
                mail.setMailTypeId(pagemail.getMailTypeId());
                mail.setMailTypeName(pagemail.getMailTypeName());
                model.addAttribute("mailnum", aoaMailnumbers);
                model.addAttribute("typelist", aoaTypeLists);
                model.addAttribute("statuslist", aoaStatusLists);
                model.addAttribute("mail", mail);
                model.addAttribute("id", "回复");
            } else if (("转发").equals(huifu)) {
                //找到该邮件信息
                Pagemail pagemail = mailService.WriteAttachmentListByFileId(id);
                if (pagemail.getAttachment_Id() != null) {
                    AttachmentList attachmentList = mailService.selectAttachmentListByFileId(pagemail.getAttachment_Id());
                    model.addAttribute("file", attachmentList);
                }
                pagemail.setInReceiverList(null);
                model.addAttribute("mailnum", aoaMailnumbers);
                model.addAttribute("typelist", aoaTypeLists);
                model.addAttribute("statuslist", aoaStatusLists);
                model.addAttribute("mail", pagemail);
                model.addAttribute("id", "转发");
            } else if (("编辑").equals(huifu)) {
                System.out.println("编辑");
                //编辑
                //找到该邮件信息
                Pagemail pagemail = mailService.WriteAttachmentListByFileId(id);
                if (pagemail.getAttachment_Id() != null) {
                    AttachmentList attachmentList = mailService.selectAttachmentListByFileId(pagemail.getAttachment_Id());
                    model.addAttribute("file", attachmentList);
                }
                model.addAttribute("mailnum", aoaMailnumbers);
                model.addAttribute("typelist", aoaTypeLists);
                model.addAttribute("statuslist", aoaStatusLists);
                model.addAttribute("mail", pagemail);
                model.addAttribute("id", "编辑");
            }

        } else {
            model.addAttribute("mailnum", aoaMailnumbers);
            model.addAttribute("typelist", aoaTypeLists);
            model.addAttribute("statuslist", aoaStatusLists);
            model.addAttribute("id", "写信");
        }
        //通讯录
        addressList(model, session);
        return "mail/wirtemail";
    }

    /*
  @Description : 第一次通讯录
  @Date   :  2021/11/24
  @Param  : [model]
  @return : void
 */
    public void addressList(Model model, HttpSession session) {

        AoaUser aoaUser = (AoaUser) session.getAttribute("user");

        /*
              通讯录
        */
        int page = 1, size = 5;
        String baseKey = "";
        //计数
        int totalElements = auditService.MailFindUserInfoByName(aoaUser.getUserId(), baseKey, page, 0, 0).size();
        //页数
        int totalPages = totalElements % size == 0 ? totalElements / size : totalElements / size + 1;
        List<AoaUser> users = auditService.MailFindUserInfoByName(aoaUser.getUserId(), baseKey, (page - 1), size, 0);
        Map<String, Object> mapPage = new HashMap<>();
        mapPage.put("totalElements", totalElements);
        mapPage.put("totalPages", totalPages);
        mapPage.put("number", page);
        mapPage.put("size", size);
        model.addAttribute("page", mapPage);
        model.addAttribute("url", "reciversmail");
        model.addAttribute("userInfo", users);
    }


    /*
   @Description : 通讯录
   @Date   :  2021/11/24
   @Param  : [model, baseKey, page, size]
   @return : java.lang.String
  */
    @RequestMapping("reciversmail")
    public String reciver(Model model, HttpSession session,
                          @RequestParam(value = "baseKey", defaultValue = "") String baseKey,
                          @RequestParam(value = "page", defaultValue = "1") int page,
                          @RequestParam(value = "size", defaultValue = "5") int size) {

        AoaUser aoaUser = (AoaUser) session.getAttribute("user");

        int totalElements = auditService.MailFindUserInfoByName(aoaUser.getUserId(), baseKey, page, 0, 0).size();
        int totalPages = totalElements % size == 0 ? totalElements / size : totalElements / size + 1;

        List<AoaUser> userInfos = auditService.MailFindUserInfoByName(aoaUser.getUserId(), baseKey, (page - 1) * size, size, 0);
        model.addAttribute("userInfo", userInfos);

        Map<String, Object> mapPage = new HashMap<>();
        mapPage.put("totalElements", totalElements);
        mapPage.put("totalPages", totalPages);
        mapPage.put("number", page);
        mapPage.put("size", size);
        model.addAttribute("page", mapPage);
        model.addAttribute("url", "reciversmail");
        return "common/recivers";

    }

    /**
     * 发送邮件
     *
     * @throws IOException
     * @throws IllegalStateException
     */

    @RequestMapping("pushmail")
    public String push(@RequestParam("file") MultipartFile file, HttpSession session,
                       Inmaillist inmaillist, String fasong, String mailNumberid
    ) throws IllegalStateException, IOException {
        System.out.println(inmaillist.getInReceiver());

        AoaUser aoaUser = (AoaUser) session.getAttribute("user");
        System.out.println("mailNumberid = " + mailNumberid);
        //上传附件
        String[] str = inmaillist.getInReceiver().split(";");
        System.out.println(Arrays.toString(str));
        if (file.getSize() != 0) {

            String shuffix = oosUtil.getPicSuffix(file.getOriginalFilename());
            long size = file.getSize();
            String type = file.getContentType();
            AoaAttachmentList attachment = new AoaAttachmentList();
            attachment.setAttachmentName(file.getOriginalFilename());
            attachment.setAttachmentPath(oosUtil.getImgUrl(oosUtil.uploadImg2Oss(file)));
            attachment.setAttachmentSize(size + "");
            attachment.setAttachmentType(type);
            attachment.setAttachmentShuffix(shuffix);
            attachment.setModel("mail");
            attachment.setUploadTime(DateUtils.getDateTimeString());
            attachment.setUserId(aoaUser.getUserId().toString());

            newProcessService2.inertAttachment(attachment);
            System.out.println(attachment.getAttachmentId());
            inmaillist.setMailFileid(attachment.getAttachmentId());
            System.out.println("ppID = " + inmaillist.getMailFileid());

        }

        String name = null;
        AoaAttendsList attaid = null;
        AoaMailnumber number = null;
        StringTokenizer st = null;

        Date date = new Date();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd :hh:mm:ss");

        inmaillist.setMailCreateTime(dateFormat.format(date));
        inmaillist.setDel(0);
        inmaillist.setMailInPushUserId(aoaUser.getUserId());
        inmaillist.setMailStar(0);
        Mailreciver mailreciver = new Mailreciver();

        if (("发送").equals(fasong)) {
            inmaillist.setMailPush(1);
            System.out.println("inmaillist = " + inmaillist);
            if (inmaillist.getMailNumberid() == 0) {
                inmaillist.setMailNumberid(null);
                mailService.sendEmail(inmaillist);
                mailreciver.setIsRead(0);
                mailreciver.setMailId(inmaillist.getMailId());
                mailreciver.setIsStar(0);
                mailreciver.setIsDel(0);
                System.out.println("mailreciver = " + mailreciver);
                for (int i = 0; i < str.length; i++) {
                    mailreciver.setMailReciverId(userService.selectUserByUserName(str[i]).getUserId());//接收人;
                    mailService.sendReciver(mailreciver);
                }

            } else {
            }

        } else {
            if (inmaillist.getMailNumberid() == 0) {
                //存草稿
                inmaillist.setMailPush(0);
                System.out.println("inmaillist = " + inmaillist);
                inmaillist.setMailNumberid(null);
                mailService.sendEmail(inmaillist);
            } else {
                mailService.sendEmail(inmaillist);
            }


        }

        return "redirect:/mail";
    }

    //文件下载
    @RequestMapping("attachmentDownload")
    public String downfile(Integer attachmentId,
                           Model model) {
        System.out.println("attachmentId = " + attachmentId);

        String accessKey = constantProperties.getKeyid();
        String secretKey = constantProperties.getKeysecret();
        String bucket = constantProperties.getBucketname();
        String endPoint = constantProperties.getEndpoint();

        AoaAttachmentList aoaAttachmentList = mailService.selectAoaAttachmentListById(attachmentId);

        System.out.println("aoaAttachmentList = " + aoaAttachmentList);
        String attachmentName = aoaAttachmentList.getAttachmentName().substring(0, aoaAttachmentList.getAttachmentName().lastIndexOf("."));
        String fileUrl = aoaAttachmentList.getAttachmentPath().substring(45);

        OSSClient client = new OSSClient(endPoint, accessKey, secretKey);

        String filePath = System.getProperty("user.dir");
        String newFilePath = filePath + "\\src\\main\\resources\\static\\image\\" + attachmentName;
        System.out.println("newFilePath = " + newFilePath);
        String url = "t/" + fileUrl;
        System.out.println("url = " + url);
        client.getObject(new GetObjectRequest(bucket, url), new File(newFilePath));

        client.shutdown();


        return "forword:filemanage";
    }


    @RequestMapping("star")
    public String starEmail(Model model, String ids, String title) {
        System.out.println("title = " + title);
        String[] str = ids.split(",");
        System.out.println(Arrays.toString(str));
        for (int i = 0; i < str.length; i++) {
            if (mailService.selectByMailId(Integer.parseInt(str[i])).getMailStar() == 1) {
                mailService.statEmail(0, Integer.parseInt(str[i]));
            } else {
                mailService.statEmail(1, Integer.parseInt(str[i]));
            }
        }
        return "redirect:mailbody";
    }

    @RequestMapping("watch")
    public String watch(Model model, String ids, String title, HttpSession session) {
        AoaUser user = (AoaUser) session.getAttribute("user");
        System.out.println("title = " + title);
        String[] str = ids.split(",");
        System.out.println(Arrays.toString(str));
        if (("收件箱").equals(title)) {
            for (int i = 0; i < str.length; i++) {
                if (mailService.findbyReciverIdAndmailId(user.getUserId(), Integer.parseInt(str[i])).getIsRead() == 1) {
                    mailService.isRead(user.getUserId(), Integer.parseInt(str[i]), 0);
                } else {
                    mailService.isRead(user.getUserId(), Integer.parseInt(str[i]), 1);
                }
            }
        }

        return "redirect:mailbody";
    }

}
